package com.letv.android.client.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

import com.letv.android.client.album.controller.LongWatchController;
import com.letv.core.utils.LogInfo;

/**
 * 点播页view
 * 
 * @author zhuqiao
 *
 */
public class AlbumPlayContainView extends RelativeLayout {
    private LongWatchController mLongWatchController;
    public static final byte KEYBOARD_STATE_SHOW = -3;
    public static final byte KEYBOARD_STATE_HIDE = -2;
    public static final byte KEYBOARD_STATE_INIT = -1;
    private boolean mHasInit;
    private boolean mHasKeybord;
    private int mHeight;
    private OnKeyboardChangeListener mListener;

    public AlbumPlayContainView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public AlbumPlayContainView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AlbumPlayContainView(Context context) {
        super(context);
    }

    /**
     * set keyboard state listener
     */
    public void setOnKeyboardChangeListener(OnKeyboardChangeListener listener) {
        LogInfo.log("Emerson", "setOnKeyboardChangeListener listener=" + listener);
        this.mListener = listener;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (!mHasInit) {
            mHasInit = true;
            mHeight = b;
            if (mListener != null) {
                mListener.onKeyBoardStateChange(KEYBOARD_STATE_INIT);
            }
        } else {
            mHeight = mHeight < b ? b : mHeight;
        }
        if (mListener == null) {
            return;
        }
        if (mHasInit && mHeight > b) {
            mHasKeybord = true;
            if (mListener != null) {
                mListener.onKeyBoardStateChange(KEYBOARD_STATE_SHOW);
            }
        }
        if (mHasInit && mHasKeybord && mHeight == b) {
            mHasKeybord = false;
            LogInfo.log("Emerson", "hide keyboard.......mListener=" + mListener);
            if (mListener != null) {
                mListener.onKeyBoardStateChange(KEYBOARD_STATE_HIDE);
            }
        }
    }

    public interface OnKeyboardChangeListener {

        void onKeyBoardStateChange(int state);
    }

    public void setLongWatchController(LongWatchController controller) {
        mLongWatchController = controller;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            if (mLongWatchController != null) {
                mLongWatchController.cancelLongTimeWatch();
            }
        }
        return super.onInterceptTouchEvent(ev);
    }

}
